package com.example.chasshelper;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chasshelper.adapter.ClassroomTeaAdapter;
import com.example.chasshelper.adapter.MessageAdapter;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Classroom;
import com.example.chasshelper.model.Message;
import com.example.chasshelper.model.Solution;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.model.Test;
import com.example.chasshelper.utils.DaoUtils;

import java.util.List;

/*从ClassroomTeaAdapter跳转过来
 * 教师课堂页面
 * */
public class ClassroomTeaActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = ClassroomTeaActivity.class.getSimpleName();
    private final Activity mContext = ClassroomTeaActivity.this;
    //控件
    private TextView tv_classroomId;
    private TextView tv_classroomName;
    private TextView tv_classroomTime;
    private TextView tv_classroomState;
    private TextView tv_classroomSignFlag;
    private TextView tv_classroomTestFlag;
    private RecyclerView rv_discussArea;
    private EditText et_chat;

    private LinearLayout ll_refresh;
    private LinearLayout ll_startClass;
    private LinearLayout ll_endClass;
    private LinearLayout ll_startSignIn;
    private LinearLayout ll_endSignIn;
    private LinearLayout ll_ask;
    private LinearLayout ll_startTest;
    private LinearLayout ll_endTest;
    private ImageView iv_send;

    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();
    //adapter
    private MessageAdapter messageAdapter;
    //数据
    private Teacher teacher;
    private Classroom classroom;
    private List<Message> messageList;
    private List<Test> testList;
    //定时刷新：2s
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {
            this.update();
            handler.postDelayed(this, 1000 * 2);// 间隔2秒
        }
        void update() {
            //刷新页面
            classroom = classroomDao.findById(classroom.getClassroomId());
            messageList = messageDao.findByClassroomId(classroom);
            //回显
            tv_classroomId.setText("课堂编号："+classroom.getClassroomId());
            tv_classroomName.setText("课堂名："+classroom.getClassroomName());
            tv_classroomTime.setText("创建时间："+classroom.getClassroomTime());
            tv_classroomState.setText("上课状态："+classroom.getClassroomState());
            tv_classroomSignFlag.setText("签到状态："+classroom.getClassroomSignFlag());
            tv_classroomTestFlag.setText("测验状态："+classroom.getClassroomTestFlag());
            messageAdapter.setList(messageList);
            messageAdapter.notifyDataSetChanged();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classroom_tea);
        //线程
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        //初始化界面
        initUI();
        //初始化数据
        initData();
        //两秒后调用此Runnable对象
        handler.postDelayed(runnable, 1000 * 2);

    }
    @Override
    protected void onDestroy() {
        //停止刷新
        handler.removeCallbacks(runnable);
        super.onDestroy();
    }


    private void initUI() {
        //绑定控件
        tv_classroomId = findViewById(R.id.tv_classroomId);
        tv_classroomName = findViewById(R.id.tv_classroomName);
        tv_classroomTime = findViewById(R.id.tv_classroomTime);
        tv_classroomState = findViewById(R.id.tv_classroomState);
        tv_classroomSignFlag = findViewById(R.id.tv_classroomSignFlag);
        tv_classroomTestFlag = findViewById(R.id.tv_classroomTestFlag);
        rv_discussArea = findViewById(R.id.rv_discussArea);
        et_chat = findViewById(R.id.et_chat);

        ll_refresh = findViewById(R.id.ll_refresh);
        ll_startClass = findViewById(R.id.ll_startClass);
        ll_endClass = findViewById(R.id.ll_endClass);
        ll_startSignIn = findViewById(R.id.ll_startSignIn);
        ll_endSignIn = findViewById(R.id.ll_endSignIn);
        ll_ask = findViewById(R.id.ll_ask);
        ll_startTest = findViewById(R.id.ll_startTest);
        ll_endTest = findViewById(R.id.ll_endTest);
        iv_send = findViewById(R.id.iv_send);

        //点击事件
        ll_refresh.setOnClickListener(this);
        ll_startClass.setOnClickListener(this);
        ll_endClass.setOnClickListener(this);
        ll_startSignIn.setOnClickListener(this);
        ll_endSignIn.setOnClickListener(this);
        ll_ask.setOnClickListener(this);
        ll_startTest.setOnClickListener(this);
        ll_endTest.setOnClickListener(this);
        iv_send.setOnClickListener(this);
    }

    private void initData() {
        //接收数据
        teacher = (Teacher) getIntent().getSerializableExtra("teacher");
        classroom = (Classroom) getIntent().getSerializableExtra("classroom");
        classroom = classroomDao.findById(classroom.getClassroomId());
        messageList = messageDao.findByClassroomId(classroom);
        testList = classroomDao.findTest(classroom);
        //回显
        tv_classroomId.setText("课堂编号：" + classroom.getClassroomId());
        tv_classroomName.setText("课堂名：" + classroom.getClassroomName());
        tv_classroomTime.setText("创建时间：" + classroom.getClassroomTime());
        tv_classroomState.setText("上课状态：" + classroom.getClassroomState());
        tv_classroomSignFlag.setText("签到状态：" + classroom.getClassroomSignFlag());
        tv_classroomTestFlag.setText("测验状态：" + classroom.getClassroomTestFlag());
        //adapter初始化，并向adapter传递数据
        messageAdapter = new MessageAdapter(mContext, messageList, classroom, teacher, null);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        //从底部开始加载加载数据
        linearLayoutManager.setStackFromEnd(true);
        rv_discussArea.setLayoutManager(linearLayoutManager);
        rv_discussArea.setAdapter(messageAdapter);

    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {

            case R.id.ll_startClass://开始上课
                if (classroom.getClassroomState().equals("进行中")) {
                    Toast.makeText(mContext, "正在上课", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (classroom.getClassroomState().equals("已结束")) {
                    Toast.makeText(mContext, "已下课", Toast.LENGTH_SHORT).show();
                    return;
                }
                classroomDao.startClassroom(classroom);
                break;
            case R.id.ll_endClass://下课
                if (classroom.getClassroomState().equals("已结束")) {
                    Toast.makeText(mContext, "已下课", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (classroom.getClassroomState().equals("未开始")) {
                    Toast.makeText(mContext, "未上课", Toast.LENGTH_SHORT).show();
                    return;
                }
                //结束签到
                if (classroom.getClassroomSignFlag().equals("进行中")) {
                    classroomDao.endSignIn(classroom);
                    Toast.makeText(mContext, "结束签到", Toast.LENGTH_SHORT).show();
                }
                classroomDao.endClassroom(classroom);
                Toast.makeText(mContext, "下课", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_startSignIn://开始签到
                //先判断课堂状态
                if (classroom.getClassroomState().equals("进行中")) {
                    classroomDao.startSignIn(classroom);
                } else {
                    Toast.makeText(mContext, "不可更改", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.ll_endSignIn://结束签到
                //先判断课堂状态和签到状态
                if (classroom.getClassroomState().equals("进行中") && classroom.getClassroomSignFlag().equals("进行中")) {
                    classroomDao.endSignIn(classroom);
                } else {
                    Toast.makeText(mContext, "不可更改", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.iv_send://发送消息:
                //先判断课堂状态
                if (classroom.getClassroomState().equals("进行中")) {
                    messageDao.insert(et_chat.getText().toString(), classroom, teacher, null);
                    et_chat.setText("");
                    refresh();
                } else {
                    Toast.makeText(mContext, "不可更改", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.ll_startTest://发布测验:
                if (classroom.getClassroomState().equals("进行中") && classroom.getClassroomTestFlag().equals("未开始")) {
                    classroomDao.startTest(classroom);
                    solutionDao.insert(classroom);
                } else {
                    Toast.makeText(mContext, "不可更改", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.ll_endTest://结束测验:
                classroomDao.endTest(classroom);
                break;
            case R.id.ll_ask://点名提问
                break;
            case R.id.ll_refresh://刷新页面
                break;
        }
    }

    //刷新页面
    private void refresh() {
        classroom = classroomDao.findById(classroom.getClassroomId());
        messageList = messageDao.findByClassroomId(classroom);
        //回显
        tv_classroomId.setText("课堂编号："+classroom.getClassroomId());
        tv_classroomName.setText("课堂名："+classroom.getClassroomName());
        tv_classroomTime.setText("创建时间："+classroom.getClassroomTime());
        tv_classroomState.setText("上课状态："+classroom.getClassroomState());
        tv_classroomSignFlag.setText("签到状态："+classroom.getClassroomSignFlag());
        messageAdapter.setList(messageList);
        messageAdapter.notifyDataSetChanged();
        //定位到最后一条
        rv_discussArea.scrollToPosition(messageAdapter.getItemCount() - 1);
    }
}